package com.intct.hbase.ddl.table;

import com.intct.hbase.ddl.namespace.AbsNamespace;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gufg
 * @since 2025-07-10 14:22
 */
public class Create extends AbsNamespace {

    /*
    命名空间名称
     */
    private String namespace;

    /*
    声明表名
     */
    private String tableName;

    /*
    多列族多版本
     */
    private Map<String, Integer> familyMap = new HashMap<>();

    public Create(String namespace, String tableName, Map<String, Integer> familyMap) {
        this.namespace = namespace;
        this.tableName = tableName;
        this.familyMap = familyMap;
    }

    // create 'ns:t1', {NAME => 'f1', VERSIONS => 5}
    @Override
    public void execCommand(Admin admin) throws IOException {
        TableName hbaseTableName = TableName.valueOf(namespace, tableName);
        // 判断表是否在
        if (!admin.tableExists(hbaseTableName)) {
            // 构造表描述
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(hbaseTableName);

//            String familyName = "f1"; //f1 => 3 f2 => 5
//
//            // 列族描述
//            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(familyName.getBytes());
//
//            // 设置列族版本参数
//            columnFamilyDescriptorBuilder.setMaxVersions(3);
//
//            // 将列族描述增加到表描述中
//            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

            List<ColumnFamilyDescriptor> columnFamilyDescriptorList = new ArrayList<>();

            for (Map.Entry<String, Integer> entry : familyMap.entrySet()) {
                String familyName = entry.getKey();
                Integer version = entry.getValue();
                // 列族描述
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(familyName.getBytes());
                // 设置列族版本参数
                columnFamilyDescriptorBuilder.setMaxVersions(version);
                // 将配置后的列族描述增加到列表中
                columnFamilyDescriptorList.add(columnFamilyDescriptorBuilder.build());
            }

            // 将列族描述增加到表描述中
            tableDescriptorBuilder.setColumnFamilies(columnFamilyDescriptorList);

            // 创建二维数组
            byte[][] splitKeys = new byte[4][];
            splitKeys[0] = "10".getBytes();
            splitKeys[1] = "20".getBytes();
            splitKeys[2] = "30".getBytes();
            splitKeys[3] = "40".getBytes();

            // 创建表，并且是多分区
            admin.createTable(tableDescriptorBuilder.build(), splitKeys);
        }
    }

    public static void main(String[] args) throws IOException {
        Map<String, Integer> familyMap = new HashMap<>();
        familyMap.put("f1", 3);
        familyMap.put("f2", 5);
        familyMap.put("f3", 6);
        new Create("intct82", "t001", familyMap).start();
    }
}
